package com.kob.backend.controller.aution;

import com.kob.backend.dto.AuctionDto;
import com.kob.backend.pojo.Auction;
import com.kob.backend.service.auction.AuctionService;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <pre>{@code
 *
 * }</pre>
 *
 * @author Summerday
 * @date 2023/3/17
 */
@RequestMapping("/auction")
@RestController
public class AuctionController {

    @Resource
    AuctionService auctionService;

    @GetMapping("/{id}")
    public Map<String, Auction> getAuctionById(@PathVariable Integer id) {
        Auction auction = auctionService.getAuctionById(id);
        Map<String, Auction> res = new HashMap<>();
        res.put("auction", auction);
        return res;
    }

    @GetMapping("/list")
    public Map<String, List<Auction>> getAuctions() {
        Map<String, List<Auction>> res = new HashMap<>();
        res.put("auctions",  auctionService.list());
        return res;
    }

    @GetMapping("/ida/{id}")
    public Map<String, Object> startIDA(@PathVariable Integer id) {
        long start = System.currentTimeMillis();
        Map<String, Object> map = auctionService.startIDA(id);
        long end = System.currentTimeMillis();
        map.put("time_cost", end-start);
        System.out.println(end-start);//返回毫秒值
        return map;
    }

    @PostMapping("/register")
    public Map<String, Object> registerAuction(@RequestBody AuctionDto auctionDto) {
        Auction auction = auctionService.register(auctionDto);
        Map<String, Object> map = new HashMap<>();
        map.put("auction", auction);
        map.put("id", auction.getId());
        return map;
    }
}
